package cn.tedu.design;
/*本类用于实现单例设计模式方案1:饿汉式*/
public class Singleton1 {
    public static void main(String[] args) {
        MySingle single1 = MySingle.getSingle();
        MySingle single2 = MySingle.getSingle();
        MySingle single3 = MySingle.getSingle();

        //用==检验是否是同一个对象
        System.out.println(single1==single2);
        System.out.println(single1);
        System.out.println(single2);
    }
}

class MySingle{
    /*1.构造方法私有化的目的:为了防止外界随意实例化本类对象*/
    private MySingle(){}

    private static MySingle single = new MySingle();
    //为了不通过对象,直接调用本方法,
    public static MySingle getSingle(){
        return single;
    }
}
